// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "Lele_Math.h"


void ULele_Math::FeetDecalToActor(ADecalActor* decal, AActor* targetActor)
{
	FVector orange;
	FVector extends;
	targetActor->GetActorBounds(false, orange, extends);
	//get the size of the actor
	FVector targetSize = extends - orange;
	targetSize.X = fabsf(targetSize.X);
	targetSize.Y = fabsf(targetSize.Y);
	targetSize.Z = fabsf(targetSize.Z);

	decal->GetActorBounds(false, orange, extends);
	FVector decalSize = extends - orange;
	decalSize.X = fabsf(decalSize.X);
	decalSize.Y = fabsf(decalSize.Y);
	decalSize.Z = fabsf(decalSize.Z);

	//scal the decal to the properte size
	FVector scaler;
	scaler.X = decalSize.X / targetSize.X;
	scaler.Y = decalSize.Y / targetSize.Y;
	scaler.Z = decalSize.Z / targetSize.Z;

	decal->SetActorScale3D(scaler);
}

FVector ULele_Math::ProjectPointToLine(FVector linePoint, FVector lineDirection, FVector sourcePoint)
{
	FVector b = sourcePoint - linePoint;
	return linePoint + FVector::DotProduct(b, lineDirection)*lineDirection/lineDirection.Size();
}

void ULele_Math::SnapeToObject(float distance, AActor* originObj, AActor* beSnapedObj)
{
	FVector right = originObj->GetActorRightVector();
	FVector left = FVector(right.X*-1, right.Y*-1, right.Z);
	FVector ALocation = originObj->GetActorLocation();
	FVector BLocation = beSnapedObj->GetActorLocation();
	if (FVector::DotProduct((BLocation-ALocation),right)>0)
	{
		beSnapedObj->SetActorLocation(right*distance + ALocation);
	}
	else
	{
		beSnapedObj->SetActorLocation(left*distance + ALocation);
	}
}

void ULele_Math::SnapeToObjectSide(float distance, FVector direction, AActor* originObj, AActor* beSnapedObj)
{
	beSnapedObj->SetActorLocation(direction*distance + originObj->GetActorLocation());
}

FVector ULele_Math::SelectVector(FVector selector, FVector a, FVector b)
{
	float ra = FVector::DotProduct(selector, a);
	float rb = FVector::DotProduct(selector, b);

	if (ra>=rb)
	{
		return a;
	}
	return b;

}

FVector ULele_Math::InverseVectorXY(FVector source)
{
	return FVector(source.X*-1, source.Y*-1, source.Z);
}


